<?php
/**
 * 
 * 绘制view
 * @author Leo Ning <leo.ning@like18.com>
 *
 */
class Response {
	public static $url;
	private static $ocontent = '';
	private static $inited = false;
	public static function begin($url) {
		if (self::$inited) {
			return;
		}
		self::$inited = true;
		if (!@ini_get ( 'zlib.output_compression' ) && @ob_get_status ()) {
			@ob_end_clean ();
		}
		@ob_start ( array ('Response', 'ob_out_handler' ) );
		self::$url = apply_filter ( 'parse_request_url', $url );
		if (defined ( 'OUT_GZIP' ) && OUT_GZIP && extension_loaded ( "zlib" )) {
			$gzip = @ini_get ( 'zlib.output_compression' );
			if (! $gzip) {
				@ini_set ( 'zlib.output_compression', 1 );
			}
		}
	}
	/**
	 * 禁用浏览器缓存
	 */
	public static function nocache() {
		$headers = array ('Expires' => 'Wed, 11 Jan 1984 05:00:00 GMT', 'Last-Modified' => gmdate ( 'D, d M Y H:i:s' ) . ' GMT', 'Cache-Control' => 'no-cache, must-revalidate, max-age=0', 'Pragma' => 'no-cache' );
		foreach ( $headers as $header => $val ) {
			@header ( $header . ': ' . $val );
		}
	}
	/**
	 * 输出错误信息.
	 *
	 * 如果当前在调试模式下,输出信息,反之设置头响应信息.
	 * 
	 * @static
	 *
	 * @param string $msg
	 * 要输出的错误信息
	 * @param int $status
	 * 头响应信息状态
	 */
	public static function error($msg, $status = null, $force = false) {
		if (! $force && defined ( 'DEBUG' ) && DEBUG) {
			echo $msg;
			self::close ();
		} else {
			if (! is_null ( $status )) {
				status_header ( $status );
				if ($status == 404 && file_exists ( TEMPLATE_PATH . 'system/404.tpl' )) {
					$view = new TemplateView ( array ('msg' => $msg ), 'system/404.tpl' );
					echo $view->render ( null, null );
				}
			}
			self::close ( true );
		}
	}
	/**
	 * 
	 * 显示错误页面
	 * @param string $msg
	 * @param string $url
	 * @param int $redirect
	 */
	public static function errorPage($msg, $url='', $redirect = 0) {
		if (file_exists ( TEMPLATE_PATH . 'system/error.tpl' )) {
			$view = new TemplateView ( array ('error_msg' => $msg, 'url' => $url, 'num' => $redirect ), 'system/error.tpl' );
			echo $view->render ( null, null );
		} else {
			echo "<html><head></head><body>";
			echo "<p>", $msg, "</p>";
			echo "</body></html>";
		}
		self::close ( true );
	}
	/**
	 * 跳转
	 * 
	 * @param string $location
	 * 要转到的网址
	 * @param array $args
	 * 参数
	 * @param int $status
	 * 响应代码
	 * 
	 */
	public static function redirect($location, $args = "", $status = 302) {
		global $is_IIS;
		if (! $location) {
			return false;
		}
		if (! empty ( $args ) && is_string ( $args )) {
			if (strpos ( $location, '?' ) !== false) {
				$location .= '&' . $args;
			} else {
				$location .= '?' . $args;
			}
		}
		if ($is_IIS) {
			@header ( "Refresh: 0;url=$location" );
		} else {
			if (php_sapi_name () != 'cgi-fcgi') {
				status_header ( $status ); // This causes problems on IIS and some
			// FastCGI setups
			}
			@header ( "Location: $location", true, $status );
		}
		self::close ( true );
	}
	/**
	 *
	 *
	 * 此方法不应该直接调用，用于ob_start处理output buffer中的内容。
	 * 
	 * @param string $content        	
	 * @return string
	 */
	public static function ob_out_handler($content) {
		$content = apply_filter ( 'filter_output_content', $content, self::$url );
		self::$ocontent .= $content;
		return $content;
	}
	/**
	 *
	 *
	 * 关闭响应，将内容输出的浏览器，同时触发after_content_output勾子
	 * 
	 * @uses fire
	 */
	public static function close($exit = true) {
		if ($exit) {
			exit ();
		} else {
			fire ( 'after_content_output', self::$ocontent, self::$url );			
		}
	}
}